package com.ruoyi.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.group.entity.YytGroupActivity;
import com.ruoyi.common.group.entity.YytGroupSkuRelation;
import com.ruoyi.common.group.service.YytGroupActivityService;
import com.ruoyi.common.group.service.YytGroupSkuOrderRelationService;
import com.ruoyi.common.group.service.YytGroupSkuRelationService;
import com.ruoyi.common.oms.dao.YytOmsOrderItemMapper;
import com.ruoyi.common.oms.entity.YytOmsOrderItem;
import com.ruoyi.utils.ResponseResult;
import com.ruoyi.vo.GroupPayInfoListVo;
import com.ruoyi.vo.GroupSkuListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author MrPing
 * @since 2022-07-19
 */
@RestController
@RequestMapping("/group")
public class YytGroupActivityController {

    @Autowired
    private YytGroupActivityService yytGroupActivityService;

    @Autowired
    private YytGroupSkuRelationService yytGroupSkuRelationService;

    @Autowired
    private YytGroupSkuOrderRelationService yytGroupSkuOrderRelationService;

    @Autowired
    private YytOmsOrderItemMapper yytOmsOrderItemMapper;

    /**
     * 获取拼团数据
     * @param status
     * @return
     */
    @GetMapping("/getGroupList")
    public ResponseResult getGroupList(@RequestParam("status") Integer status){
        QueryWrapper<YytGroupActivity> wrapper = new QueryWrapper<>();
        wrapper.eq("status", status == 0?1:0);
        if(status == 1){
            wrapper.ge("start_time", LocalDateTime.now()).orderByAsc("start_time");
            List<YytGroupActivity> list = yytGroupActivityService.list(wrapper);
            if(list.size() > 0){
                return ResponseResult.success(yytGroupSkuRelationService.getSkuByGroupIdList(list.get(0).getId()));
            }
        }else {
            YytGroupActivity groupActivity = yytGroupActivityService.getOne(wrapper, false);
            if (groupActivity != null){
                List<GroupSkuListVo> skuByGroupIdList = yytGroupSkuRelationService.getSkuByGroupIdList(groupActivity.getId());
                for (int i = 0; i < skuByGroupIdList.size(); i++){
                    skuByGroupIdList.get(i).setPayInfoListVos(yytOmsOrderItemMapper.getBuyUserList(skuByGroupIdList.get(i).getSkuId()));
                }
                return ResponseResult.success(skuByGroupIdList);
            }
        }
        return ResponseResult.success(new ArrayList<>());
    }


    @GetMapping("/getGroupCountDown")
    public ResponseResult getGroupCountDown(@RequestParam("skuId") String skuId){
        YytGroupActivity activityBySkuId = yytGroupActivityService.getActivityBySkuId(skuId);
        long groupEndTime = activityBySkuId.getEndTime().toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
        long nowTime = new Date().getTime();
        long endTime = (groupEndTime - nowTime) / 1000;
        Map<String, Long> map = new HashMap<>();
        long countDay = endTime / 86400;
        long countHour = endTime % 86400 / 3600;
        long countDown = endTime % 3600 / 60;
        long secondTime = endTime % 60;
        map.put("countDay", countDay);
        map.put("countHour", countHour);
        map.put("countDown", countDown);
        map.put("secondTime", secondTime);

        return ResponseResult.success(map);
    }

    /**
     * 获取该拼团商品已经抢购了多少
     * @param skuId
     * @return
     */
    @GetMapping("getSkuNumInfo")
    public ResponseResult getSkuNumInfo(@RequestParam("skuId") String skuId){
        QueryWrapper<YytGroupSkuRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("sku_id", skuId);
        return ResponseResult.success(yytGroupSkuRelationService.getOne(wrapper, false));
    }

    /**
     * 获取拼团配送日期
     * @param skuId
     * @return
     */
    @GetMapping("/getGroupDeliveryDate")
    public ResponseResult getDeliveryDate(@RequestParam("skuId") String skuId){
        QueryWrapper<YytGroupSkuRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("sku_id", skuId);
        return ResponseResult.success(yytGroupSkuRelationService.getOne(wrapper, false));
    }


    /**
     * 获取团品的送货日期及是否已经购买
     * @param memberId
     * @param skuId
     * @return
     */
    @GetMapping("/getGroupDateBuy")
    public ResponseResult getGroupDateBuy(@RequestParam("memberId") String memberId, @RequestParam("skuId") String skuId){
        QueryWrapper<YytGroupSkuRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("sku_id", skuId);
        YytGroupSkuRelation skuRelation = yytGroupSkuRelationService.getOne(wrapper, false);
        List<YytOmsOrderItem> buyIsStatus = yytOmsOrderItemMapper.getBuyIsStatus(memberId, skuId);
        Map map = new HashMap();
        map.put("date", skuRelation.getSendDate());
        map.put("status", buyIsStatus.size() > 0?true:false);

        return ResponseResult.success(map);
    }

}
